<?php
require_once MODULE_ROOT."/class/oauth2.class.php";
global $_W,$_GPC;
load()->func('tpl');
	$acid=$_W['uniacid'];
	$rid=$_GPC["rid"];
	
	//调试
function is_weixin(){

	if ( strpos($_SERVER['HTTP_USER_AGENT'],

			'MicroMessenger') !== false ) {

			return true;

	}

	return false;

}

if(is_weixin()){
	
}else{
	
	$_W["openid"]='oU4go0msnyP2JizVuKdoNoZ1xLqo';
}
	$openid = $_W["openid"];

	$fromid=$_GPC["fromid"];//分享人openid=$_GPC["fromid"];//分享人openid
	$reply=pdo_fetch("SELECT * FROM " . tablename('dt_question_reply') . " WHERE rid = :rid ", array(':rid' => $rid));
	
	
	
	//用户表
	
	if($reply["subscribe"]==1){
		$nick=$_W['fans']['nickname'];
		$headimg=$_W['fans']['tag']['avatar'];
	}else{
		//调试
		$userinfo=$this->getUserInfo($rid);
		//print_r($userinfo); exit();
		$obj = json_decode($userinfo);
		$nick=$obj->nickname;
		$headimg=$obj->headimgurl;
    }
    
   
    
	
	//处理分享
	if($_W['isajax']){
		//print_r($_GPC);
		//添加成绩记录
		$time=date('Y-m-d',time());
		$shareparams = array("rid"=>$rid,"iacid"=>$acid, "uniacid"=>$acid,"fromuserid"=>$fromid, "openid"=>$openid,"nick"=>$nick,"headimg"=>$headimg,"addtime"=>$time);
		
			$sharetable='dt_usershare';
			pdo_insert($sharetable, $shareparams);
			
		//修改答题记录为有效
        pdo_query("update ims_dt_userpoints set status=1 where openid='$openid' and  FROM_UNIXTIME(unix_timestamp(time),'%Y-%m-%d')=FROM_UNIXTIME(unix_timestamp(now()),'%Y-%m-%d')");
        //取必选题 做记录
        $mustquest=pdo_fetch("select * from ims_dt_question where is_must=1 ORDER BY RAND()");
        $realanswer=$mustquest["answer$mustquest[realanswer]"];
        $date=date('Y-m-d',time());
        pdo_query("update ims_dt_userinfo set mustdate='$date',mustid={$mustquest[id]} where openid='$openid' ");
        
         
	    $fmdata["success"]=1;
	    $fmdata["question"]=$mustquest['question'];
	    $fmdata["realanswer"]=$mustquest["answer".$mustquest["realanswer"]];
		echo json_encode($fmdata);
		exit();
	}
	
	$userinfo_2=pdo_fetch("select * from ".tablename("dt_userinfo")." where openid=:openid and rid = :rid",array(":openid"=>$openid,":rid"=>$rid));
	
  /**
	 * 分享
	 */
	
	$sharetitle=$reply["sharetitle"];//分享标题
	$sharecontent=$reply["sharecontent"];//分享内容
	//$shareimg=$_W['attachurl'].$reply["shareimg"];//分享图片
	
	$shareimg=$headimg ? $headimg : $userinfo_2['headimg'];//分享图片
	
	$isshareadd=$reply["isshareadd"];//是否 分享增加一次
	
	
	$sharelink="";
	$huodongname=$reply["huodongname"];
	$color=$reply["color"];
	if($isshareadd==0){
		$sharelink = $_W ['siteroot'] . 'app/' . $this->createMobileUrl ( 'goshare', array ('rid' => $rid));
	}else{
		$sharelink=$_W ['siteroot'] . 'app/' . $this->createMobileUrl ( 'goshare', array ('rid' => $rid,'fromid'=>$openid));
	}
$sharelink = str_replace ( './', '', $sharelink );//分享链接
	$url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
	//$sharelink=$reply['shareurl'];
	
	//$sharetitle="我在芝麻开门赢得{$userinfo_2['points']}元现金宝藏！来芝麻开门答题拿现金！";
	$sharetitle="我在珂蓝国际【正青春—占天下】答题活动中赢得80元现金宝藏！来芝麻开门答题赢现金！";
	
	$sharecontent="我已经领取80元现金宝藏！轻松回答3道题，立得现金奖！这个活动官方认证100％真实有效，全国8068个领到的人都说好";
	
	
//	$sharelink = $_W ['siteroot'] . 'app/' . $this->createMobileUrl ( 'goshare', array ('rid' => $rid));
	

		setcookie("nick",$nick);
		setcookie("head",$headimg);
		include $this->template('goshare');
	
	
	exit ();